package com.ycsj.goldmedalnewconcept.teacher.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.ycsj.common.adapter.VpCommonAdapter;
import com.ycsj.common.base.BaseLazyFragment;
import com.ycsj.common.base.BusInfo;
import com.ycsj.common.view.vp.SuperViewPager;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.TkKeys;
import com.ycsj.goldmedalnewconcept.common.bean.SubListBean;
import com.ycsj.goldmedalnewconcept.common.bean.TkItemBean;
import com.ycsj.goldmedalnewconcept.teacher.bean.AnswerInfo;
import com.ycsj.goldmedalnewconcept.teacher.bean.ReadLvInfo;
import com.ycsj.goldmedalnewconcept.teacher.manager.TitleManager;

import java.util.ArrayList;
import java.util.List;

/**
 * 完形填空
 * Created by Administrator on 2018/9/3 0003.
 */

public class ClozeTestFragment extends BaseLazyFragment implements ViewPager.OnPageChangeListener {

    //页面对象
    private TkItemBean info;

    //题目类型
    private TextView tvType;
    //标题
    private TextView tvTitle;

    //题目内容
    private TextView tvContent;
    private SuperViewPager vp;

    //vp的页面指示器
    private TextView tvNum;

    //填充ViewPager的Framgent集合
    private List<Fragment> mFragments;

    //vp的适配器
    private VpCommonAdapter adapter;

    //以完成状态下对应的所有题型的解析和自己选择的状态
    private String[] analysis;

    //学生做题0  1学生查看解析 2老师查看题和解析-有人做(全校正确率和小题正确率都显示) 3(习题分析)老师查看题和解析-没人做,对应查看单题
    private String type;

    //当前大题在整个题目中所处的位置
    private int currPos;

    public static final ClozeTestFragment newInstance(TkItemBean item, String type, int currPos) {
        ClozeTestFragment teacherGoldFragment = new ClozeTestFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("data", item);
        bundle.putSerializable("type", type);
        bundle.putInt("currpos", currPos);
        teacherGoldFragment.setArguments(bundle);
        return teacherGoldFragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_cloze_test;
    }


    @Override
    public void lazyInit(View view, Bundle savedInstanceState) {
    }

    @Override
    protected void init(View view, Bundle savedInstanceState) {
        super.init(view, savedInstanceState);

        //获取页面对象
        initIntent();

        //初始化视图
        initView(view);

        //初始化页面数据
        initData();
    }


    /**
     * 获取页面对象
     */
    private void initIntent() {
        //未完成的数据对象
        info = (TkItemBean) getArguments().getSerializable("data");

        type = (String) getArguments().getSerializable("type");

        //获取当前大题在整个题目中所处的位置
        currPos = getArguments().getInt("currpos");

        analysis = info.getAnalysis().split("￠");

    }

    /**
     * 初始化视图
     *
     * @param view
     */
    public void initView(View view) {
        tvType = view.findViewById(R.id.tv_type);
        tvTitle = view.findViewById(R.id.tv_title);
        tvContent = view.findViewById(R.id.tv_content);
        vp = view.findViewById(R.id.vp);
        vp.addOnPageChangeListener(this);
        tvNum = view.findViewById(R.id.tv_num);
        TitleManager.setTitle(info.getTypeid(), tvType);


        //获取标题的内容
        if (TextUtils.isEmpty(info.getN_topic_name())) {
            tvType.setText(info.getN_type_name());
        } else {
            tvType.setText(info.getN_type_name() + "-" + info.getN_topic_name());
        }
    }


    /**
     * 初始化页面的数据,包含阅读的内容,以及提供选择的
     */
    private void initData() {

        if (info != null) {

            //获取题干内容
            String questionContent = info.getContent();
            tvContent.setText(questionContent);

            //获取题目选项
            List<SubListBean> questions = info.getSub_list();

            //创建底部选择答案的Fragment页面的数据集合
            List<AnswerInfo> answerInfos = new ArrayList<>();

            for (int i = 0; i < questions.size(); i++) {

                AnswerInfo inf = new AnswerInfo();

                //每个小题的解析
                try {
                    if (TextUtils.isEmpty(info.getAnalysis())) {
                        inf.explain = "略";
                    } else {
                        inf.explain = analysis[i].trim().toString();
                    }
                } catch (Exception e) {
                    inf.explain = "略";
                }
                //设置用户选项答案
                inf.chooseAnswer = questions.get(i).getStu_option();

                //设置正确答案
                inf.rightAnswer = questions.get(i).getOption();

                //设置全校正确率
                inf.proba = questions.get(i).getYes_pro();

                //选择答案的集合
                List<ReadLvInfo> readInfos = new ArrayList<>();

                List<String> list = questions.get(i).getList();

                SubListBean.ProBean probabilityBean = info.getSub_list().get(i).getPro();

                for (int j = 0; j < list.size(); j++) {

                    ReadLvInfo readLvInfo = new ReadLvInfo();
                    readLvInfo.questionIndex = letters[j];
                    readLvInfo.questionContent = list.get(j);

                    //用户选择的答案
                    readLvInfo.chooseAnswer = questions.get(i).getStu_option();

                    //该小题的正确答案
                    readLvInfo.rightAnswer = questions.get(i).getOption();

                    //每个选项的正确率
                    if (null == probabilityBean) {
                        readLvInfo.probability = "0.0%";
                    } else {
                        if (j == 0) {
                            if (!TextUtils.isEmpty(probabilityBean.getA())) {
                                readLvInfo.probability = probabilityBean.getA();
                            } else {
                                readLvInfo.probability = "0.0%";
                            }
                        } else if (j == 1) {
                            if (!TextUtils.isEmpty(probabilityBean.getB())) {
                                readLvInfo.probability = probabilityBean.getB();
                            } else {
                                readLvInfo.probability = "0.0%";
                            }
                        } else if (j == 2) {
                            if (!TextUtils.isEmpty(probabilityBean.getC())) {
                                readLvInfo.probability = probabilityBean.getC();
                            } else {
                                readLvInfo.probability = "0.0%";
                            }
                        } else if (j == 3) {
                            if (!TextUtils.isEmpty(probabilityBean.getD())) {
                                readLvInfo.probability = probabilityBean.getD();
                            } else {
                                readLvInfo.probability = "0.0%";
                            }
                        }
                    }

                    readInfos.add(readLvInfo);
                }

                inf.readLvInfos = readInfos;

                answerInfos.add(inf);
            }

            //根据获取的answerInfos来创建填充ViewPager的Fragment
            mFragments = new ArrayList<>();
            if (answerInfos.size() != 0) {
                for (int i = 0; i < answerInfos.size(); i++) {
                    mFragments.add(ItemSingleChooseFragment.newInstance(answerInfos.get(i), i, Integer.parseInt(type)));
                }

                //设置底部的VP的指示器数字
                tvNum.setText("1/" + mFragments.size());

                //设置ViewPager的数据
                adapter = new VpCommonAdapter(getChildFragmentManager(), mFragments);
                vp.setAdapter(adapter);
                vp.setOffscreenPageLimit(mFragments.size());
            }
        }
    }

    /**
     * 返回答案是 "A":"Smart. ",拆分
     * A到F对应的字母索引
     *
     * @param answer 答案选择
     */
    public ReadLvInfo putAnswer(int index, String answer) {

        if (TextUtils.isEmpty(answer)) {
            return null;
        } else {
            ReadLvInfo readLvInfo = new ReadLvInfo();
            //先用:切割
            readLvInfo.isSelected = false;
            readLvInfo.questionIndex = letters[index];
            readLvInfo.questionContent = answer;
            return readLvInfo;
        }
    }


    /**
     * ViewPager的滚动监听事件
     *
     * @param position
     * @param positionOffset
     * @param positionOffsetPixels
     */
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        tvNum.setText((position + 1) + "/" + mFragments.size());
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    /**
     * 事件处理
     *
     * @param busInfo
     */
    @Override
    public void busEvent(BusInfo busInfo) {
        super.busEvent(busInfo);
        //点击答题面板将VP位置切换成小题的位置
        if (null != busInfo && currPos == busInfo.bigPos&&busInfo.type == TkKeys.TKCOMIT) {
            //切换对应的小题位置
            vp.setCurrentItem(busInfo.postion);
        }
    }

}
